﻿using JT808.Protocol.MessageBody;
using JT808.Protocol.MessagePack;
using System;
using System.Collections.Generic;
using System.Text;

namespace JT808.Protocol.Formatters.MessageBodyFormatters
{
    public class JT808_0x8900_Bind_Formatter : IJT808MessagePackFormatter<JT808_0x8900_Bind>
    {
        public JT808_0x8900_Bind Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
        {
            JT808_0x8900_Bind jT808_0X8900_Bind = new JT808_0x8900_Bind();
            jT808_0X8900_Bind.passthroughID = reader.ReadByte();
            jT808_0X8900_Bind.operationType = reader.ReadByte();
            jT808_0X8900_Bind.tyreNumber = reader.ReadByte();
            var list = new List<JT808_0x8900_Sensor>();
            for (int i = 0; i < jT808_0X8900_Bind.tyreNumber; i++)
            {
                list.Add(new JT808_0x8900_Sensor
                {
                    sensor_Id = reader.ReadByte().ToString(),
                    toca = reader.ReadByte(),
                    tyreSerialNumber = reader.ReadByte()
                });
            }
            return jT808_0X8900_Bind;
        }

        public void Serialize(ref JT808MessagePackWriter writer, JT808_0x8900_Bind value, IJT808Config config)
        {
            writer.WriteByte(value.passthroughID);
            writer.WriteByte(value.operationType);
            writer.WriteByte(value.tyreNumber);
            foreach (var item in value.sensor)
            {
                writer.WriteHexByte(item.sensor_Id);
                writer.WriteByte(item.toca);
                writer.WriteByte(item.tyreSerialNumber);
            }
        }
    }
}
